package com.demo.rabbitmq.config;

import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author yexi
 */
@Component
public class RabbitListenerConfig implements ApplicationRunner {

    @Autowired
    private RabbitListenerEndpointRegistry container;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 不堵塞springboot容器启动
        new Thread(() -> {
            try {
                // 模拟10秒后开启
                TimeUnit.SECONDS.sleep(10);
                // 将设置为 autoStartup = "false" 的监听器启动起来
                container.start();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }).start();
    }
}
